package com.wb.base.refreshview;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wb.base.R;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;


public class HeaderView extends LinearLayout implements PtrUIHandler {

    private TextView tvHeadView;
    private ImageView ivHeadView;
    private ImageView ivHeadViewLoading;
    private AnimationDrawable animationDrawable;
    private boolean isComplete;

    public HeaderView(Context context) {
        this(context, null);
    }

    public HeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.head_view, this);
        tvHeadView = (TextView) findViewById(R.id.tvHeadView);
        ivHeadView = (ImageView) findViewById(R.id.ivHeadView);
        ivHeadViewLoading = (ImageView) findViewById(R.id.ivHeadViewLoading);
        animationDrawable = (AnimationDrawable) ivHeadViewLoading.getDrawable();
    }

    @Override
    public void onUIReset(PtrFrameLayout frame) {
//        imageView.setImageResource(R.drawable.head_0);
        tvHeadView.setText("下拉刷新");
        isComplete = false;
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        isComplete = false;
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        animationDrawable.stop();
        ivHeadView.setVisibility(View.VISIBLE);
        ivHeadViewLoading.setVisibility(View.GONE);
        tvHeadView.setText("下拉刷新");
        isComplete = false;
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        animationDrawable.stop();
        ivHeadView.setVisibility(View.VISIBLE);
        ivHeadViewLoading.setVisibility(View.GONE);
        tvHeadView.setText("刷新完成");
        isComplete = true;
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        float currentPercent = Math.min(1f, ptrIndicator.getCurrentPercent());
        if (isUnderTouch) {
            if (currentPercent >= 1) {
                ivHeadView.setVisibility(View.VISIBLE);
                ivHeadViewLoading.setVisibility(View.GONE);
                tvHeadView.setText("松开刷新");
            } else {
                ivHeadView.setVisibility(View.VISIBLE);
                ivHeadViewLoading.setVisibility(View.GONE);
                tvHeadView.setText("下拉刷新");
            }
        } else {
            if (currentPercent >= 1) {
                if (isComplete) {
                    animationDrawable.stop();
                    ivHeadView.setVisibility(View.VISIBLE);
                    ivHeadViewLoading.setVisibility(View.GONE);
                    tvHeadView.setText("刷新完成");
                } else {
                    animationDrawable.start();
                    ivHeadView.setVisibility(View.GONE);
                    ivHeadViewLoading.setVisibility(View.VISIBLE);
                    tvHeadView.setText("正在刷新...");
                }
            }
        }
    }
}
